//
//  AIHomeActivitiesView.m
//  aixianfeng
//
//  Created by wangwei on 2016/12/23.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

#import "AIHomeActivitiesView.h"

#import "AIHomeActivitiesModel.h"
//活动控制器
#import "AIHomeActivitiesController.h"

#define ButtonCount  4
#define Margin 3
@interface AIHomeActivitiesView()

@property (nonatomic,strong)NSMutableArray<UIButton *> *buttonArray;
@property (nonatomic,strong)NSMutableArray<NSString *> *urlStringArray;

@end

@implementation AIHomeActivitiesView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.buttonArray = [NSMutableArray array];
        self.urlStringArray = [NSMutableArray array];
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    for (int i = 0; i < ButtonCount; i++) {
        UIButton *btn = [UIButton buttonWithType:0];
        btn.backgroundColor = [UIColor blueColor];
        [self.buttonArray addObject:btn];
        [self addSubview:btn];
        btn.tag = i + 1;
        [btn addTarget:self action:@selector(actionButton:) forControlEvents:UIControlEventTouchUpInside];
    }

}

- (void)actionButton:(UIButton *)sender {
    //创建控制器
    AIHomeActivitiesController *activitiesCV = [AIHomeActivitiesController new];
    activitiesCV.customURL = self.urlStringArray[sender.tag - 1];
    [[sender findNavController] pushViewController:activitiesCV animated:YES];
  
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //在这里我们需要计算位置
    CGFloat btnX,btnY,btnW,btnH;
    btnX = 0;
    btnW = self.bounds.size.width;
    btnH = (self.bounds.size.height - (ButtonCount -1)*Margin)  / ButtonCount;
    
    for (int i = 0; i < self.buttonArray.count; i++) {
        btnY = i * (btnH + Margin);
        self.buttonArray[i].frame = CGRectMake(btnX, btnY, btnW, btnH);
    }
}

- (void)setActivitiesArray:(NSArray<AIHomeActivitiesModel *> *)activitiesArray {
    _activitiesArray = activitiesArray;
    
    for (int i = 0; i < activitiesArray.count; i++) {
       // NSLog(@"%@",activitiesArray[i].img);
        [self.urlStringArray addObject:activitiesArray[i].customURL];
        [self.buttonArray[i] sd_setImageWithURL:[NSURL URLWithString:self.activitiesArray[i].img] forState:UIControlStateNormal];
    }
    

}


@end














